<template>
  <BlueBackground :title="'我的地址'">
    <div>
      <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
      />
      <van-empty v-if="list.length === 0" description="当前没有地址" />
    </div>
  </BlueBackground>
</template>

<script>
import BlueBackground from "../components/BlueBackground.vue";
export default {
  name: "Address",
  data() {
    return {
      tokenString: sessionStorage.getItem("userToken"),
      appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
      chosenAddressId: "1",
      list: [],
    };
  },
  methods: {
    onAdd() {
      this.$router.push({ name: "NewAddress" });
    },
    onEdit(item, index) {
      this.$router.push({ name: "AddressEdit", query: { aid: item.aid } });
    },
    getAddress() {
      let appkey = this.appkey;
      let tokenString = this.tokenString;
      this.axios({
        method: "get",
        url: "http://www.kangliuyong.com:10002/findAddress",
        params: {
          appkey,
          tokenString,
        },
      })
        .then((res) => {
          // this.list = res.data.result;
          let arr = res.data.result;
          arr.forEach((v, i) => {
            this.list.push({
              id: v.id,
              name: v.name,
              tel: v.tel,
              address:
                v.province === v.city
                  ? `${v.province}${v.county}${v.addressDetail}`
                  : `${v.province}${v.city}${v.county}${v.addressDetail}`,
              isDefault: v.isDefault === 1 ? true : false,
              aid: v.aid,
            });
          });
        })
        .catch((err) => {});
    },
  },
  created() {
    this.getAddress();
  },
  components: {
    BlueBackground,
  },
};
</script>

<style lang="less" scoped>
.bottom-box {
  width: 100%;
  position: fixed;
  bottom: 0;
  left: 0;
  padding: 10px 5%;
  background: #fff;
}
/deep/.van-button--large {
  width: 90%;
}
/deep/.van-button--danger {
  background-color: rgb(12, 52, 186);
  border: 1px solid rgb(12, 52, 186);
}
/deep/.van-radio__icon {
  display: none;
}
/deep/.van-tag--danger {
  background-color: rgb(12, 52, 186);
}
</style>